home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / CameraModel.as next >
Text File  |  2008-11-04  |  5KB  |  187 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import flash.events.ErrorEvent;
  7.    import flash.events.NetStatusEvent;
  8.    import flash.events.SecurityErrorEvent;
  9.    import flash.media.Camera;
  10.    import flash.media.Microphone;
  11.    import flash.media.Video;
  12.    import flash.net.NetConnection;
  13.    import flash.net.NetStream;
  14.    import flash.net.ObjectEncoding;
  15.    import flash.utils.clearInterval;
  16.    import flash.utils.setInterval;
  17.    
  18.    public class CameraModel implements ModelInterface
  19.    {
  20.        
  21.       
  22.       private var stream:NetStream;
  23.       
  24.       private var interval:Number;
  25.       
  26.       private var connection:NetConnection;
  27.       
  28.       private var camera:Camera;
  29.       
  30.       private var model:Model;
  31.       
  32.       private var microphone:Microphone;
  33.       
  34.       private var position:Number;
  35.       
  36.       private var video:Video;
  37.       
  38.       public function CameraModel(param1:Model)
  39.       {
  40.          var mod:Model = param1;
  41.          super();
  42.          model = mod;
  43.          try
  44.          {
  45.             camera = Camera.getCamera();
  46.             microphone = Microphone.getMicrophone();
  47.             video = new Video(320,240);
  48.          }
  49.          catch(err:Error)
  50.          {
  51.             model.sendEvent(ModelEvent.ERROR,{"message":"No webcam found on this computer."});
  52.          }
  53.          connection = new NetConnection();
  54.          connection.objectEncoding = ObjectEncoding.AMF0;
  55.          connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  56.          connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  57.          quality(model.config["quality"]);
  58.       }
  59.       
  60.       public function stop() : void
  61.       {
  62.          position = 0;
  63.          video.attachCamera(null);
  64.          clearInterval(interval);
  65.          if(stream)
  66.          {
  67.             stream.publish(null);
  68.          }
  69.       }
  70.       
  71.       public function pause() : void
  72.       {
  73.          video.attachCamera(null);
  74.          if(stream)
  75.          {
  76.             stream.publish(null);
  77.             stream.attachAudio(null);
  78.             stream.attachCamera(null);
  79.          }
  80.          clearInterval(interval);
  81.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  82.       }
  83.       
  84.       private function statusHandler(param1:NetStatusEvent) : void
  85.       {
  86.          if(param1.info.code == "NetConnection.Connect.Success")
  87.          {
  88.             stream = new NetStream(connection);
  89.             stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  90.             play();
  91.          }
  92.          model.sendEvent(ModelEvent.META,{"info":param1.info.code});
  93.       }
  94.       
  95.       public function volume(param1:Number) : void
  96.       {
  97.       }
  98.       
  99.       private function errorHandler(param1:ErrorEvent) : void
  100.       {
  101.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  102.       }
  103.       
  104.       public function load() : void
  105.       {
  106.          position = model.playlist[model.config["item"]]["start"];
  107.          model.mediaHandler(video);
  108.          if(model.config["streamer"])
  109.          {
  110.             connection.connect(model.config["streamer"]);
  111.          }
  112.          else
  113.          {
  114.             play();
  115.          }
  116.       }
  117.       
  118.       private function timeInterval() : void
  119.       {
  120.          var _loc1_:* = undefined;
  121.          position = Math.round(position * 10 + 1) / 10;
  122.          _loc1_ = model.playlist[model.config["item"]]["duration"];
  123.          if(_loc1_ > 0)
  124.          {
  125.             if(position >= _loc1_)
  126.             {
  127.                clearInterval(interval);
  128.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  129.             }
  130.             else
  131.             {
  132.                model.sendEvent(ModelEvent.TIME,{
  133.                   "position":position,
  134.                   "duration":_loc1_
  135.                });
  136.             }
  137.          }
  138.       }
  139.       
  140.       public function play() : void
  141.       {
  142.          video.attachCamera(camera);
  143.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  144.          interval = setInterval(timeInterval,100);
  145.          if(stream)
  146.          {
  147.             stream.publish(model.playlist[model.config["item"]]["file"]);
  148.             stream.attachAudio(microphone);
  149.             stream.attachCamera(camera);
  150.          }
  151.       }
  152.       
  153.       public function seek(param1:Number) : void
  154.       {
  155.          position = param1;
  156.          clearInterval(interval);
  157.          play();
  158.       }
  159.       
  160.       public function quality(param1:Boolean) : void
  161.       {
  162.          if(param1 == true)
  163.          {
  164.             camera.setMode(480,360,25);
  165.             video.smoothing = true;
  166.             video.deblocking = 4;
  167.             model.sendEvent(ModelEvent.META,{
  168.                "framerate":25,
  169.                "height":360,
  170.                "width":480
  171.             });
  172.          }
  173.          else
  174.          {
  175.             camera.setMode(240,180,12);
  176.             video.smoothing = false;
  177.             video.deblocking = 1;
  178.             model.sendEvent(ModelEvent.META,{
  179.                "framerate":12,
  180.                "height":180,
  181.                "width":240
  182.             });
  183.          }
  184.       }
  185.    }
  186. }
  187.